<?php
define('WPADMIN_URL', get_option('siteurl').'/wp-admin/');

/**
 * register sidebar
 */
if ( function_exists('register_sidebar') ) {
	register_sidebars(3, array(
		'before_widget' => '<li id="%1$s" class="widget %2$s">',
		'after_widget' => '</li>',
		'before_title' => '<h3>',
		'after_title' => '</h3>',
	));
}

/**
 * Format post time to time ago.
 */
function post_time($args='', $isAgo=true){
	$output = null;
	if( $isAgo )
	{
		$ago = current_time('timestamp') - get_the_time('U');
	    $periods = array('second', 'minute', 'hour', 'day', 'week', 'month', 'year', 'decade');
	    $lengths = array('60', '60', '24', '7', '4.35', '12', '10');
	    for ($j=0;$ago>=$lengths[$j];$j++)
	      $ago /= $lengths[$j];
	    $ago = round($ago);
	    if ( $ago!=1 ) $periods[$j].= 's';
	    $output = $ago.' '.$periods[$j].' ago';
	}
	else
	{
		$output = the_date($args);
	}

    echo $output;
}

/**
 * List hottest posts in recent one month. 
 */
function top_posts($num=5){
	global $wpdb;
	$output = null;
	if(!function_exists('akpc_init')){
		$result = $wpdb->get_results
		(
			"SELECT comment_count,ID,post_title FROM $wpdb->posts ".
			"WHERE EXTRACT(YEAR_MONTH FROM now())=EXTRACT(YEAR_MONTH FROM post_date_gmt) ".
			"ORDER BY comment_count DESC LIMIT 0, {$num}"
		);
		if (sizeof($result)>0){
			$output .= '<ul id="top_posts_ul">';
			foreach ($result as $post) {
				$postid = $post->ID;
				$title = $post->post_title;
				$commentcount = $post->comment_count;
				if ( $commentcount!=0 ) {
					$output .= '<li class="top_post_li"><a href="'.get_permalink($postid).'" title="'.$title.'">'.$title.'-'.$commentcount.'</a></li>';
				}
			}
			$output .= '</ul>';
		}
		else
		{
			$output = 'maybe nofound or empty.';
		}
	}
	else
	{
		$result = $wpdb->get_results
		(
			"SELECT $wpdb->posts.comment_count,$wpdb->posts.ID,$wpdb->posts.post_title,$wpdb->ak_popularity.total ".
			"FROM $wpdb->posts,$wpdb->ak_popularity ".
			"WHERE ".//(EXTRACT(YEAR_MONTH FROM now())=EXTRACT(YEAR_MONTH FROM $wpdb->posts.post_date_gmt)) AND ".
				"$wpdb->posts.ID=$wpdb->ak_popularity.total ".
			"ORDER BY $wpdb->ak_popularity.total DESC LIMIT 0, {$num}"
		);
		foreach ($result as $post) {
				$postid = $post->ID;
				$title = $post->post_title;
				$commentcount = $post->comment_count;
				$output .= '<a href="'.get_permalink($postid).'" title="'.$title.'">'.$title.'-'.$commentcount.'</a><br />';
			}
	}
	echo $output;
}

/**
 * get copyright years
 */
function get_copyright()
{
	global $wpdb;
	$post_datetimes = $wpdb->get_results("SELECT YEAR(post_date_gmt) AS year FROM $wpdb->posts WHERE post_date_gmt > 1970 ORDER BY post_date_gmt ASC");
	$firstpost_year = $post_datetimes[0]->year;
	$lastpost_year = $post_datetimes[count($post_datetimes)-1]->year;
	
	$copyright = __('Copyright &copy; ', 'inove') . $firstpost_year;
	if($firstpost_year != $lastpost_year)
		$copyright .= '-'. $lastpost_year;
		
	return $copyright;
}

/*
 * check comments if waiting for moderation.
 */ 
function check_comments()
{
	if(current_user_can('moderate_comments')) {
		global $wpdb;
		$awaiting_mod = $wpdb->get_var("SELECT COUNT(*) FROM $wpdb->comments WHERE comment_approved = '0'");
		if($awaiting_mod)
		echo '(<a href="'.WPADMIN_URL.'moderation.php" class="moderation">'.$awaiting_mod.'</a>)';
	}
}

/*
 * check plugins if its has new version.
 */ 
function check_plugins()
{
	if (current_user_can('activate_plugins')) {
		$update_plugins = get_option( 'update_plugins' );
		$update_count = count( $update_plugins->response );
		if($update_count!="0")
			echo '('.number_format_i18n($update_count).')';
	}
}
?>
